import { AppComponentBase } from '@shared/common/app-component-base';
import { Component, OnInit, Injector } from '@angular/core';
import { DictionaryDTO, ServiceProxy } from '@shared/service-proxy/service-proxies';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
@Component({
  selector: 'app-sms-config',
  templateUrl: './sms-config.component.html',
  styleUrls: ['./sms-config.component.less']
})
export class SmsConfigComponent extends AppComponentBase implements OnInit {
  dictionary: DictionaryDTO = new DictionaryDTO();
  dataSet = [];
  validateForm: FormGroup;
  isVisible: boolean = false;
  isOkLoading: boolean = false;
  constructor(injector: Injector, private fb: FormBuilder, private _service: ServiceProxy) {
    super(injector);
  }


  ngOnInit() {
    this.validateForm = this.fb.group({
      itemValue: [null, [Validators.required]]
    });
    this.getSmsRule();
  }

  getSmsRule() {
    this._service.getDictionaryByDictionaryTypeUsingGET("sms_type").subscribe(result => {
      this.dataSet = result.result.fieldList;
    })
  }

  showModal(id: string): void {
    this.dictionary = new DictionaryDTO();
    if (id) {
      this._service.getDictionaryUsingGET(id).subscribe(result => {
        this.dictionary = result.result;
      })
    }
    this.isVisible = true;
  }

  handleOk(): void {
    this.isOkLoading = true;

    this._service.updateDictionaryUsingPUT(this.dictionary.id, this.dictionary).subscribe(result => {
      if (result.success) {
        this.getSmsRule();
        this.message.success(this.l("save_success"));
        this.isVisible = false;
        this.isOkLoading = false;
      }
      else {
        this.message.info(this.l(result.error.message));

        this.isOkLoading = false;
      }
    })
  }

  handleCancel(): void {
    this.isVisible = false;
  }

  handleUsing(data: DictionaryDTO) {
    if (data.itemValue == "true") {
      data.itemValue = "false";
    }
    else if (data.itemValue == "false") {
      data.itemValue = "true";
    }
  }
}

